from PySide6.QtWidgets import QWidget, QHBoxLayout, QPushButton, QFileDialog, QMessageBox, QSizePolicy

class ControlPanel(QWidget):
    def __init__(self, tts_engine, parent=None):
        super().__init__(parent)
        self.tts_engine = tts_engine
        self.auto_speech_enabled = False
        self.setup_ui()
        
    def setup_ui(self):
        # 创建按钮布局
        button_layout = QHBoxLayout(self)
        button_layout.setContentsMargins(0, 5, 0, 5)  # 减小上下边距
        
        # 设置固定高度
        self.setMaximumHeight(50)  # 限制高度
        self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)  # 水平可伸缩，垂直固定
        
        # 选择视频按钮
        self.open_video_button = QPushButton("选择视频")
        button_layout.addWidget(self.open_video_button)
        
        # 开始/暂停按钮
        self.start_button = QPushButton("开始")
        self.start_button.setEnabled(False)
        button_layout.addWidget(self.start_button)

        # 语音TTS按钮
        self.tts_button = QPushButton("语音播报")
        self.tts_button.setEnabled(self.tts_engine.is_available())
        button_layout.addWidget(self.tts_button)
        
        # 添加自动播报切换按钮
        self.auto_speech_button = QPushButton("自动播报: 关")
        self.auto_speech_button.setEnabled(self.tts_engine.is_available())
        button_layout.addWidget(self.auto_speech_button)
        
        # 添加连接树莓派按钮
        self.connect_raspberry_button = QPushButton("连接树莓派")
        button_layout.addWidget(self.connect_raspberry_button)
        
    def open_video_dialog(self):
        """打开选择视频文件对话框"""
        file_path, _ = QFileDialog.getOpenFileName(
            self,
            "选择视频文件",
            "",
            "视频文件 (*.mp4 *.avi *.mov)"
        )
        return file_path
        
    def toggle_auto_speech(self):
        """切换自动语音播报功能"""
        if not self.tts_engine.is_available():
            QMessageBox.warning(self, "语音引擎不可用", "请确保已安装pyttsx3库。")
            return False
            
        self.auto_speech_enabled = not self.auto_speech_enabled
        
        if self.auto_speech_enabled:
            self.auto_speech_button.setText("自动播报: 开")
        else:
            self.auto_speech_button.setText("自动播报: 关")
            
        return self.auto_speech_enabled
    
    def update_start_button(self, is_processing):
        """更新开始/暂停按钮的状态"""
        if is_processing:
            self.start_button.setText("暂停")
        else:
            self.start_button.setText("开始")
